package com.ruicar.afs.cloud.apply.pre.loan.mq.receiver;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.ruicar.afs.cloud.apply.business.service.ApplyRecordDetailsService;
import com.ruicar.afs.cloud.apply.common.entity.ApplyCustBaseInfo;
import com.ruicar.afs.cloud.apply.common.entity.WorkflowRecordDetails;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyCustBaseInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.service.impl.ApplyOrderInfoServiceImpl;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ProcessTypeEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ApproveOpenDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ApproveOpenResult;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 接收审批结果-审核中
 *
 * @author 耿继辉
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyApproveInfoDealProcess implements AfsMqBizProcessor<ApproveOpenResult> {
    private final ApplyCustBaseInfoService applyCustBaseInfoService;
    private final ApplyRecordDetailsService applyRecordDetailsService;
    private final ApplyOrderInfoServiceImpl applyOrderInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(ApproveOpenResult entity) throws Exception {
        ApproveOpenDto approveOpenDto = entity.getApproveOpenDto();
        WorkflowRecordDetails recordDetails = new WorkflowRecordDetails();
        ApplyCustBaseInfo custBaseInfo = applyCustBaseInfoService.getCustBaseInfo(entity.getApplyNo(), ApplyConstants.PRINCIPAL_BORROWER);
        if (ObjectUtil.isNotNull(approveOpenDto)) {
            //常规审批-审批中
            if (ProcessTypeEnum.GENERA_APPROVE.getCode().equals(approveOpenDto.getProcessType())) {
                if (StringUtil.isNotEmpty(approveOpenDto.getOpenResult())) {
                    log.info("{},接收到审批结果", entity.getApplyNo());
                    if (WhetherEnum.YES.getCode().equals(approveOpenDto.getOpenResult())) {
                        recordDetails.setBusinessNo(entity.getApplyNo());
                        recordDetails.setApproveDate(approveOpenDto.getOpenTime());
                        recordDetails.setApproveResult(approveOpenDto.getOpenResult());
                        recordDetails.setProcessId(approveOpenDto.getStageId());
                        recordDetails.setCreateBy(custBaseInfo.getCreateBy());
                        recordDetails.setCreateTime(new Date());
                        applyRecordDetailsService.save(recordDetails);
                        log.info("申请编号:{},保存审批记录", entity.getApplyNo());
                        //更新订单状态为审核中
                        applyOrderInfoService.updateApplyStatus(entity.getApplyNo(), ApplyConstants.APPLY_STATUS_APPROVE);
                        log.info("常规审批-申请编号:{},更新订单状态为审核中", entity.getApplyNo());
                    }
                }
            } else if (ProcessTypeEnum.FORMAL_REVIEW.getCode().equals(approveOpenDto.getOpenResult())) {
                //正式复议-审核中状态
                if (WhetherEnum.YES.getCode().equals(approveOpenDto.getOpenResult())) {
                    recordDetails.setBusinessNo(entity.getApplyNo());
                    recordDetails.setApproveDate(approveOpenDto.getOpenTime());
                    recordDetails.setApproveResult(approveOpenDto.getOpenResult());
                    recordDetails.setProcessId(approveOpenDto.getStageId());
                    recordDetails.setCreateBy(custBaseInfo.getCreateBy());
                    recordDetails.setCreateTime(new Date());
                    log.info("申请编号:{},保存审批记录", entity.getApplyNo());
                    //更新订单状态为审核中
                    applyOrderInfoService.updateApplyStatus(entity.getApplyNo(), ApplyConstants.APPLY_STATUS_APPROVE);
                    log.info("常规审批-申请编号:{},更新订单状态为审核中", entity.getApplyNo());
                }
            }
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_APPROVE_OPEN_RESULT;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }
}
